<?php

//应付查询
class PayableApp extends MallbaseApp
{


    function index()
    {
        $data = array();
        $pageIndex = empty($_REQUEST['pageIndex']) ? 1 : trim($_REQUEST['pageIndex']);
        $start = date('Y-m-01', strtotime(date("Y-m-d")));
        $end = date('Y-m-d', strtotime(date("Y-m-d")));
        $bdate = empty($_REQUEST['bdate']) ? $start : trim($_REQUEST['bdate']);
        $edate = empty($_REQUEST['edate']) ? $end : trim($_REQUEST['edate']);
        //$wxNum = empty($_REQUEST['wxNum']) ? '123' : trim($_REQUEST['wxNum']);
        $wxNum = isset($_COOKIE['wxid'])?$_COOKIE['wxid']:'';
        if(empty($wxNum)){
            echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
            echo "<script>alert('授权未成功');  window.location.href='http://dskc.csrsfwl.com/index.php?app=login'; </script>  ";//使用weixin.php的页面
        }
        if ($this->checkUrl(WEB_SERVICE)) {
            $client = new soapclient(WEB_SERVICE);
            $client->soap_defencoding = 'utf-8';
            $client->decode_utf8 = false;
            $client->xml_encoding = 'utf-8';
            $param = array(
                'json' => '{"pageIndex":"' . $pageIndex . '","bdate":"' . $bdate . '","edate":"' . $edate . '","wxNum": "' . $wxNum . '"}');
//            echo $param['json'];
            $result = $client->__Call("QueryPayableList", array($param));

            if (is_soap_fault($result)) {
                trigger_error("SOAP Fault: (faultcode: {$result->faultcode}, faultstring: {$result->faultstring})", E_USER_ERROR);
            } else {
                $result = json_decode(json_encode($result->QueryPayableListResult), TRUE);
                $result = preg_replace('/([\r\n])/', '', $result);
                $result = json_decode($result, true);
                if ($result['status'] == 1) {
                    $data = $result['result'];
                    $dataCount = $result['dataCount'];
                } else {
                    if ($result['result'] == '该用户未登录') {
                        echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
                        echo "<script>alert('请先绑定微信'); window.location.href='index.php?app=login&hist=payable&wxNum=".$wxNum."'; </script>  ";
                        exit();
                    } elseif (strstr($result['result'], '该用户无权')) {
                        echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
                        echo "<script>alert('$result[result]'); window.location.href='index.php?app=menu'; </script>  ";
                        exit();
                    } else {
                        echo "<script>alert('".$result['result']."'); </script>  ";
                    }

                }

            }
        }
        $this->import_resource(array('script' => 'jquery.ui/jquery.ui.js,jquery.ui/i18n/' . i18n_code() . '.js',
            'style' => 'jquery.ui/themes/ui-lightness/jquery.ui.css'));
        $this->assign('bdate', $bdate);
        $this->assign('edate', $edate);
        if ($data) {
            $this->assign('payables', $data);
            $page = $this->_get_page();
            $page['item_count'] = $dataCount;
            $this->_format_pageweb($page);
            $this->assign('page_info', $page);
        }
        $this->display('payable.index.html');
    }

    //明细
    function detail()
    {
        $key = isset($_GET['id']) ? trim($_GET['id']) : '';
        $data = array();
        $pageIndex = empty($_REQUEST['pageIndex']) ? 1 : trim($_REQUEST['pageIndex']);
        $bdate = empty($_REQUEST['bdate']) ? '' : trim($_REQUEST['bdate']);
        $edate = empty($_REQUEST['edate']) ? '' : trim($_REQUEST['edate']);
        //$wxNum = empty($_REQUEST['wxNum']) ? '123' : trim($_REQUEST['wxNum']);
        $wxNum = isset($_COOKIE['wxid'])?$_COOKIE['wxid']:'';
        if(empty($wxNum)){
            echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
            echo "<script>alert('授权未成功');  window.location.href='http://dskc.csrsfwl.com/index.php?app=login'; </script>  ";//使用weixin.php的页面
        }
        if ($this->checkUrl(WEB_SERVICE)) {
            $client = new soapclient(WEB_SERVICE);
            $client->soap_defencoding = 'utf-8';
            $client->decode_utf8 = false;
            $client->xml_encoding = 'utf-8';
            $param = array(
                'json' => '{"pageIndex":"' . $pageIndex . '","key":"' . $key . '","bdate":"' . $bdate . '","edate":"' . $edate . '","wxNum": "' . $wxNum . '"}');
            $result = $client->__Call("QueryPayableInfo", array($param));
            if (is_soap_fault($result)) {
                trigger_error("SOAP Fault: (faultcode: {$result->faultcode}, faultstring: {$result->faultstring})", E_USER_ERROR);
            } else {
                $result = json_decode(json_encode($result->QueryPayableInfoResult), TRUE);
                $result = json_decode($result, true);

                if ($result['status'] == 1) {
                    $data = $result['result'];
                    $dataCount = $result['dataCount'];
                    foreach ($data as $k => $val) {
                        $data[$k]['unitid'] = $key;
                    }
                }

            }
        }
        $this->assign('bdate', $bdate);
        $this->assign('edate', $edate);
        if ($data) {
            $this->assign('payables', $data);
            $page = $this->_get_page();
            $page['item_count'] = $dataCount;
            $this->_format_pageweb($page);
            $this->assign('page_info', $page);
        }
        $this->display('payable.detail.html');
    }

    function more()
    {
        $key = isset($_REQUEST['id']) ? trim($_REQUEST['id']) : '';
        $tid = isset($_REQUEST['tid']) ? trim($_REQUEST['tid']) : '';
        $data = array();
        $bdate = empty($_REQUEST['bdate']) ? '' : trim($_REQUEST['bdate']);
        $edate = empty($_REQUEST['edate']) ? '' : trim($_REQUEST['edate']);
        $pageIndex = empty($_REQUEST['pageIndex']) ? 1 : trim($_REQUEST['pageIndex']);
        //$wxNum = empty($_REQUEST['wxNum']) ? '123' : trim($_REQUEST['wxNum']);
        $wxNum = isset($_COOKIE['wxid'])?$_COOKIE['wxid']:'';
        if(empty($wxNum)){
            echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
            echo "<script>alert('授权未成功');  window.location.href='http://dskc.csrsfwl.com/index.php?app=login'; </script>  ";//使用weixin.php的页面
        }
        if ($this->checkUrl(WEB_SERVICE)) {
            $client = new soapclient(WEB_SERVICE);
            $client->soap_defencoding = 'utf-8';
            $client->decode_utf8 = false;
            $client->xml_encoding = 'utf-8';
            $param = array(
                'json' => '{"pageIndex":"' . $pageIndex . '","key":"' . $key . '","bdate":"' . $bdate . '","edate":"' . $edate . '","wxNum": "' . $wxNum . '"}');
            $result = $client->__Call("QueryPayableInfo", array($param));
            if (is_soap_fault($result)) {
                trigger_error("SOAP Fault: (faultcode: {$result->faultcode}, faultstring: {$result->faultstring})", E_USER_ERROR);
            } else {
                $result = json_decode(json_encode($result->QueryPayableInfoResult), TRUE);
                $result = json_decode($result, true);
                if ($result['status'] == 1) {
                    foreach ($result['result'] as $k => $val) {
                        if ($val['tid'] == $tid) {
                            $data = $val;
                            break;
                        }
                    }

                }


            }
        }
        if ($data) {
            $this->assign('payable', $data);
        }
        $this->display('payable.more.html');
    }

}
